package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dto.TProductDTO;
import com.qf.entity.TProduct;
import com.qf.entity.TProductDesc;
import com.qf.mapper.TProductDescMapper;
import com.qf.mapper.TProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiuLiang
 * @Date 2020/3/13
 */
@Controller
@RequestMapping("/back/product")
public class ProductController {

    @Autowired
    private TProductMapper productMapper;

    @Autowired
    private TProductDescMapper productDescMapper;

    @RequestMapping("list")
    @ResponseBody
    public PageInfo<TProductDTO> list(int pageNum, int pageSize){
        PageInfo<TProductDTO> pageInfo = null;
        List<TProductDTO> productDTOList = new ArrayList<>();

        List<TProductDesc> productDescList = productDescMapper.selectAll();

        PageHelper.startPage(pageNum,pageSize);
        List<TProduct> productList = productMapper.selectAll();
        for (TProduct product:productList){
            for (TProductDesc productDesc:productDescList){
                if (product.getPid() == productDesc.getPid()){
                    TProductDTO productDTO = new TProductDTO();
                    BeanUtils.copyProperties(product,productDTO);
                    productDTO.setDesc(productDesc.getPdesc());
                    break;
                }
            }
        }
        return new PageInfo<>(productDTOList);
    }
}
